package me.mononin.dilechat.activities;

import android.content.Context;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.target.ImageViewTarget;

import net.qiujuer.genius.ui.compat.UiCompat;

import butterknife.BindView;
import me.mononin.common.common.app.Activity;
import me.mononin.common.common.app.Fragment;
import me.mononin.dilechat.R;
import me.mononin.dilechat.fragments.account.AccountTrigger;
import me.mononin.dilechat.fragments.account.LoginFragment;
import me.mononin.dilechat.fragments.account.RegisterFragment;

public class UserActivity extends Activity implements AccountTrigger{

    private Fragment mCurrentFragment;
    private Fragment mLoginFragment;
    private Fragment mRegisterFragment;

    @BindView(R.id.im_bg)
    ImageView mBg;

    @Override
    protected int getContentId() {
        return R.layout.activity_user;
    }

    public static void show(Context context){
        context.startActivity(new Intent(context,UserActivity.class));
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mCurrentFragment = mLoginFragment = new LoginFragment();
        //初始化fragment
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.lay_container,mCurrentFragment)
                .commit();

        Glide.with(this)
                .load(R.drawable.bg_src_tianjin)
                .centerCrop()
                .into(new ImageViewTarget<GlideDrawable>(mBg) {
                    @Override
                    protected void setResource(GlideDrawable resource) {
                        //通过Glide拿到drawable
                        Drawable drawable = resource.getCurrent();
                        //通过适配类包装drawable
                        drawable = DrawableCompat.wrap(drawable);
                        //对Drawable进行着色
                        drawable.setColorFilter
                                (UiCompat.getColor(getResources(),R.color.colorAccent),
                                        PorterDuff.Mode.SCREEN);
                        this.view.setImageDrawable(drawable);
                    }
                });
    }


    @Override
    public void triggerView() {
        Fragment fragment;
        //判断当前是否为登录界面
        if (mCurrentFragment==mLoginFragment){
            if (mRegisterFragment==null){
                //如果注册界面为空的话，新建一个
                mRegisterFragment = new RegisterFragment();
            }
            fragment = mRegisterFragment;
        }else {
            fragment = mLoginFragment;
        }
        //切换当前Fragment
        mCurrentFragment = fragment;
        //切换
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.lay_container,mCurrentFragment)
                .commit();
    }
}
